package com.zmwx.auth.application.controller;

import com.google.common.base.Preconditions;
import com.zmwx.auth.application.convert.AuthRoleDTOToConverter;
import com.zmwx.auth.application.dto.AuthRoleDTO;
import com.zmwx.auth.common.entity.Result;
import com.zmwx.auth.domain.entityDO.AuthRoleBO;
import com.zmwx.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Resource
    AuthRoleDomainService authRoleDomainService;
    /**
     * 新增角色
     *
     * @return
     */
    @RequestMapping("/add")
    public Result<Boolean> addRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("新增角色,参数:{}", authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(), "角色key不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOToConverter.INSTANCE.convertDTOToBo(authRoleDTO);
            boolean result = authRoleDomainService.addRole(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("注册失败", e.getMessage(), e);
            return Result.fail("注册失败");
        }
    }
    /**
     * 修改角色
     *
     * @return
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("update角色,参数:{}", authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色ID不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOToConverter.INSTANCE.convertDTOToBo(authRoleDTO);
            boolean result = authRoleDomainService.updateRole(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("更新失败", e.getMessage(), e);
            return Result.fail("更新失败");
        }
    }
    /**
     * 删除角色
     *
     * @return
     */
    @RequestMapping("/delete")
    public Result<Boolean> deleteRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("删除角色,参数:{}", authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色ID不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOToConverter.INSTANCE.convertDTOToBo(authRoleDTO);
            boolean result = authRoleDomainService.deleteRole(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }
}